import logging
import re
import time

import uvicorn
from fastapi import Response, Request, Query, APIRouter, FastAPI
from starlette.middleware import Middleware
from starlette.middleware.sessions import SessionMiddleware
from starlette.staticfiles import StaticFiles
from routers.api_router import router as api_router

app = FastAPI(middleware=[
    Middleware(SessionMiddleware, secret_key="YOUR_SECRET_KEY")
])

# 设置日志格式
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
)


# 自定义静态文件类，只允许特定后缀
class CustomStaticFiles(StaticFiles):
    def lookup_path(self, path: str) -> tuple[str | None, dict[str, str] | None]:
        if not re.search(r"\.(html|css|js|ico|jpg|png)$", path, re.IGNORECASE):
            return None, None
        return super().lookup_path(path)


app.include_router(api_router, prefix="/api", tags=["Base"])

# 挂载静态文件目录
app.mount("/", CustomStaticFiles(directory="templates"), name="templates")

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True, log_level="debug", workers=12)
